package com.glc.gao.gavin.rxjavademo.operator;

import android.view.View;

import com.glc.gao.gavin.rxjavademo.R;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;

public class ZipActivity extends BaseActivity {
    @Override
    public int getDescribeText() {
        return R.string.describe_zip;
    }

    @Override
    protected int getContextView() {
        return R.layout.activity_zip;
    }

    public void executeZip(View view) {
        Observable.zip(Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> e) throws Exception {
                print("Observable One" + 111);
                e.onNext(111);
                print("Observable One" + 222);
                e.onNext(222);
                print("Observable One" + 333);
                e.onNext(333);
                print("Observable One" + 444);
                e.onNext(444);

            }
        }), Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                print("Observable Two AAA");
                e.onNext("AAA");
                print("Observable Two BBB");
                e.onNext("BBB");
                print("Observable Two CCC");
                e.onNext("CCC");
            }
        }), new BiFunction<Integer, String, String>() {
            @Override
            public String apply(Integer integer, String s) throws Exception {

                print("BiFunction " + "integer: " + integer + " string: " + s);
                return s + " " + integer;
            }

        }).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                print("accept " + s);
            }
        });
    }
}
